home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Floppyshop 2
/
Floppyshop - 2.zip
/
Floppyshop - 2.iso
/
diskmags
/
0022-3.564
/
dmg-3313
/
news.txt
/
pascal3.asc
< prev
next >
Wrap
Text File
|
1989-04-05
|
11KB
|
320 lines
--------------------------------------------------------------
PPPPPP AAAAA SSSS S CCCCC AAAAA LL
PP PP AA AA SS SS CC C AA AA LL
PP PP AA AA SS CC AA AA LL
PPPPPP AAAAAAA SSSSS CC AAAAAAA LL
PP AA AA SS CC AA AA LL
PP AA AA SS SS CC C AA AA LL
PP AA AA S SSSS CCCCC AA AA LLLLLLL
===============================================================
TUTORIAL PART 3 - BY DEREK CLEGG
---------------------------------------------------------------
Last time I left you two short (but sweet) problems to solve in Pascal.
As yet I have not delved into the art of problem solving and so that is
what I intend to do now. To demonstrate the problem solving method
programmers use (called stepwise refinement or top down design), I will
use problem one from last STEN as the problem to be solved!!
What 'Top down design' really is, is the breaking up of a problem down
into smaller sub-problems and repeatedly breaking each sub-problem into a
further sub-problem until all of the sub-problems are in or very near the
target language you will be coding your program in. The actual language
used in stepwise refinement is called psuedo code and has very few 'key
words'.
Lets start by analysing the problem to arrive at our 'Top level design'
of the problem. The first thing you should notice is that an asterisk
marks the end of the names in the file also each name is separated by a
slash ie '/'. As with nearly all program specifications, there have been
some things omitted such as it doesn't say that there are carriage
returns after each name. So for the purpose of this exercise we'll assume
that all the names in the file will be on the same line ie the file is
just one big long stream of single characters.
From this you should arrive at a top-level design similar to:-
BEGIN
Open files;
WHILE data remains DO
Copy a name; 1.1
END.
At any stage of your design of a solution to a given problem you should
be able to look at your design and see that it will solve the task. At
the moment are design is very brief and simplistic yet if you look at it,
it does actually solve the problem! So now we must take our sub-problem,
'Copy a name', and see can we refine it a little bit. Never try to get
down to your chosen language to soon.
1.1 Refinement of 'Copy a name'.
--------------------------------
BEGIN
WHILE its not the end of the name DO
copy a single character 1.1.1
END;
Look at your refinement to copy a name and ask yourself does this
refinement solve the problem of actually copying one name. Although it is
simplistic and can be refined further still, it does actually solve the
task of copying one name. So now lets look at the process of copying one
character.
1.1.1 Refinement of 'Copy a character'.
----------------------------------------
BEGIN
Read in character from input file;
Write out character to output file
END;
Thus we have reached the stage where our psuedo code can be easily
converted into Pascal (or any language for that matter) and so we can now
write the actual Pascal code. We will need an input file of ASCII
characters which we'll call 'Namesin.dat' and the program will send the
output to a file called 'Namesout.dat'.
PROGRAM CopyNames (NamesIn, NamesOut);
CONST EndOfData = '*';
EndOfName = '/';
PROCEDURE CopyACharacter;
VAR Ch : CHAR; (* ~Note~ Ch is a local variable *)
BEGIN
READ ( NamesIn, Ch );
WRITE ( NamesOut, Ch )
END;
PROCEDURE CopyAName;
BEGIN
WHILE NamesIn∧ <> EndOfName DO
CopyACharacter
END;
PROCEDURE OpenFiles;
BEGIN
(* Open input file for reading from *)
RESET ( NamesIn );
(* Open output file for writimg to *)
REWRITE ( NamesOut )
END;
BEGIN (* Main program *)
OpenFiles;
WHILE NamesIn∧ <> EndOfData DO
BEGIN
CopyAName; (* Refinement 1.1 *)
END
END (* End of main *).
Here follows the Pascal code for a solution to task 2 from STEN 9.
PROGRAM Distances ( Input, Output );
CONST Yard = 36;
Foot = 12;
VAR Feet, Yards, Inches, Count : INTEGER;
Tot_Yards, Tot_Feet, Tot_Inches : INTEGER;
(* The procedures used! *)
PROCEDURE GetDistance;
BEGIN
(* Prompt user to enter a measurement *)
WRITELN('Enter your measurement in inches please.');
(* Accept measurement from user *)
READLN( Inches )
END;
PROCEDURE Process;
BEGIN
Yards := Inches DIV Yard;
Inches := Inches MOD Yard;
Feet := Inches DIV Foot;
Inches := Inches MOD Foot;
(* Update running totals. *)
Tot_Yards := Tot_yards + Yards;
Tot_Feet := Tot_feet + feet;
Tot_inches := Tot_inches + Inches
END;
PROCEDURE Output;
BEGIN
WRITELN; WRITELN;
WRITE(Yards:2,' Yards ',Feet:2,' feet and ');
WRITELN(Inches:2,' inches.')
END;
BEGIN (* Start of main program *)
FOR Count := 1 TO 10 DO
BEGIN
GetDistance;
Process;
Output
END;
WRITELN;
WRITELN('Totals are :- ');
WRITE(Tot_yards:4,' Yards ',Tot_feet:4,' feet and ');
WRITELN(Tot_inches:4,' inches.')
END.
~~~OOOO~~~
An approach to problem solving.
-------------------------------
A possible approach could be summarised inthe diagram;
___________ ___________ _________ ____
|Specify the| <-> |Analyse the| <-> |Implement| <-> |test the|
| problem | | problem | |solution | |solution|
----------- ----------- --------- --------
| | | |
| | | |
_____________________________________________________________
| D o c u m e n t a t i o n |
-------------------------------------------------------------
Specifying the problem - this involves understanding thoroughly
the problem to be solved AND defining
PRECISELY the Input and output.
Analysis - involves understanding the problem, and creating
a manual solution of the problem.
( Top-down design & stepwise refinement ).
Implementation - Pseudo code ------> target language
Debugging
Here follows a simple analysis to problem solving using stepwise
refinement.
BEGIN
Break problem into a sequence of sub-problems.
REPEAT
Break each sub-problem into a sequence of smaller
sub-problems
UNTIL each sub-problem is expressed in the target language.
Parameters.
-----------
A procedure may be used to define an action, which is required at one,
two or more points in a program.
Q - What if the same action is to be performed on different data?
eg. A delay of varying lengths may be required at different points within
a program. Where the following code is the main program, the line
delay(100) calls the procedure 'delay' and passes a value of 100 to the
procedure to use as the delay. Thus the line delay(200) calls the same
procedure 'delay' and this time passes a value of 200 as the time delay.
BEGIN
delay(100);
-------
-------
delay(200)
END.
Thus by passing a different value each time, that the delay changes, to
the procedure delay, it is avoids having to write two procedures one that
delays for 100 and one that delays for 200.
Here is the correct syntax for the delay procedure;
PROCEDURE delay ( time : Integer );
VAR Idx : Integer;
BEGIN
FOR Idx := 1 TO time DO
END;
In the above procedure the variable 'time' is actually a value parameter,
which will take the value of any integer value passed to this procedure!
On entry to the procedure 'delay' a new local variable called 'time' is
created which takes the value that was passed into the procedure. On exit,
the variable no longer exists.
~~~OOOO~~~
Next month I'll introduce you to the idea of variable parameters and I'll
expand a little on both value and variable parameters and local
variables.
i) Write a procedure which sorts two integers (which will be it's
parameters) into ascending order. Then incorporate that pro- cedure into
a program which gets three integers from the keyboard and sorts the three
numbers into ascending order.
ii) Write a procedure to output to screen a specified character a number
of times. The character to output can change each time the procedure is
called and so to can the number of times the character is to be output.
iii) Write a program to validate a date in the form DD/MM/YY where the
year lies between 1901 - 1976.
eg REPEAT
READLN ( dd,ch,mm,ch,yy );
Validate ( dd,mm,yy, Valid );
IF Valid
THEN WRITELN ( 'The date is valid!' )
ELSE WRITELN ('Invalid date!');
UNTIL yy = 0